package com.runfast.waimai.web.controller.data;

import com.runfast.common.utils.SessionContext;
import com.runfast.common.web.entity.Result;
import com.runfast.common.web.entity.ResultCode;
import com.runfast.waimai.service.RunfastBusinessDailyService;
import com.runfast.waimai.dao.model.RunfastBusinessDailyExample;
import com.runfast.waimai.dao.model.WaiMaiRunfastAgentaccounts;
import com.runfast.waimai.utils.MyUtil;
import org.apache.commons.lang3.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

/**
 * Created by WQ on 2018/6/5.
 */
@RestController
@RequestMapping("/server/data/")
public class AbnormalController {
    @Autowired
    RunfastBusinessDailyService businessDailyService;

    /**
     * @param type 1昨天 2近7天 3近30天
     * @return
     */
    @GetMapping("abnormal")
    public Result list(Integer type, @PageableDefault Pageable pageable) {
        Validate.notNull(type, "参数错误");
        if (type != 1 && type != 2 && type != 3) {
            return Result.fail(ResultCode.PARAMETER_ERROR);
        }
        WaiMaiRunfastAgentaccounts agentAccounts = SessionContext.getCurrentRunfastAgentaccounts();
        Date date1;
        Date yesterday = MyUtil.addTimeSpan(0, -1);
        if (type == 1) {
            date1 = yesterday;
        } else if (type == 2) {
            date1 = MyUtil.addTimeSpan(0, -7);
        } else {
            date1 = MyUtil.addTimeSpan(0, -30);
        }
        RunfastBusinessDailyExample example = new RunfastBusinessDailyExample();
        example.createCriteria().andAgentIdEqualTo(agentAccounts.getAgentid()).andDateBetween(date1, yesterday);
        Map<String, Object> map = businessDailyService.countByExampleWithPageableGroupByBusinessId(example, pageable);
        return Result.ok("", map);
    }
}
